package com.owdp.code.controller;

import com.owdp.code.entity.Admin;
import com.owdp.code.service.AdminService;
import com.owdp.code.utils.MD5Util;
import com.owdp.code.utils.MD5Utils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

@RestController
@RequestMapping(value = "/admin")
public class AdminController {
    @Resource
    AdminService adminService;
    //管理员登录
    @GetMapping(value = "/login")
    public String login(String anumber, String apassword) throws UnsupportedEncodingException, NoSuchAlgorithmException {
//        String encryptedPwd = MD5Util.MD5(apassword);//直接对获取的网页密码进行解密识别（简单版MD5加密，用的的是MD5Util）
        String apassword2 = adminService.login(anumber);//获取数据库的加密密码
        boolean decodePwd= MD5Utils.validpassword(apassword,apassword2);//通过调用MD5Utils工具来解对称加密，判断true or false

        System.out.println("url_pwd：" + apassword);
        System.out.println("apassword2：" + apassword2);
        System.out.println("decodePwd：" + decodePwd);
//判断密码decodePwd是true or false。
        if(decodePwd){
            return "登录成功";
        }
        return "账号或密码错误";
    }
    //管理员注册
    @PostMapping(value = "/regist")
    public String  regist(Admin admin,String anumber) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        int count=adminService.findAdminByNumber(anumber);
        if (count!=0){
            System.out.println("工号重复注册");
            return "返回重新注册";
        }
        System.out.println("用户注册成功");
        String s=admin.getApassword();//取出admin的密码
//        String apassword=MD5Util.MD5(s);//对密码进行简单版加密
        String apassword= MD5Utils.getEncryptedPwd(s);//对密码进行功能性强的MD5对称加密
        admin.setApassword(apassword);//再把加密了的密码放入admin
        adminService.regist(admin);
        return "success,注册成功";
    }
}
